package com.project.gcsj5.controller;

import com.project.gcsj5.common.Result;
import com.project.gcsj5.mapper.BookMapper;
import com.project.gcsj5.model.Book;
import com.project.gcsj5.model.User;
import com.project.gcsj5.service.BookService;
import com.project.gcsj5.service.userService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class BookController {

    @Autowired
    private BookService service;

    @GetMapping("/book/{id}")
    public Result GetByid(@PathVariable int id){
        Book book = new Book();
        book = service.get_a_book(id);
        if(book != null)
        {return Result.success(book);}
        else
        {return Result.fail();}
    }

    @GetMapping("/book_add/{id}")
    public Result add_view(@PathVariable int id){
        Boolean a = false;
        a = service.add_view(id);
        if(a != null)
        {return Result.success(a);}
        else
        {return Result.fail();}
    }

    @PostMapping("/book_stars")
    public Result add_stars(@RequestBody Map<String, Object> request){
        Boolean a = false;
        System.out.println(request);
        Integer bookId = (Integer) request.get("bookId");
        Float rating = ((Number) request.get("rating")).floatValue();
        a = service.stars_new(rating,bookId);
        if(a != null)
        {return Result.success(a);}
        else
        {return Result.fail();}
    }

    @GetMapping("/book/s01/{content}")
    public Result search_by_content(@PathVariable("content") String content){
        List<Book> commesnt = new ArrayList<>();
        commesnt = service.get_by_content(content);
        if(commesnt!=null)
        {return Result.success(commesnt);}
        else
        {return Result.fail();}
    }

    @GetMapping("/book/s02/{tags}")
    public Result search_by_tags(@PathVariable("tags") String content){
        List<Book> commesnt = new ArrayList<>();
        commesnt = service.find_by_category(content);
        if(commesnt!=null)
        {return Result.success(commesnt);}
        else
        {return Result.fail();}
    }
}
